package backend.project.gym_management.service.impl;

import backend.project.gym_management.mapper.StaffMapper;
import backend.project.gym_management.pojo.dto.*;
import backend.project.gym_management.pojo.vo.*;
import backend.project.gym_management.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class StaffServiceImpl implements StaffService {
    @Autowired
    private StaffMapper staffMapper;

    /**
     * 根据用户名或id查询用户信息
     * @param staffQuery
     * @return List<StaffSelectUserVO>
     */
    public StaffUserPageVO selectUserByNameOrUid(StaffQuery staffQuery){
        log.debug("开始处理查询用户信息请求，参数：{}", staffQuery);
        int pageNum = (staffQuery.getPageNum() - 1) * staffQuery.getPageSize();
        staffQuery.setPageNum(pageNum);
        List<StaffSelectUserVO> users = staffMapper.selectUserByNameOrUid(staffQuery);
        List<StaffSelectUserAndCourseVO> userAndCourse = new ArrayList<>();

        for (StaffSelectUserVO u : users){
            List<StaffSelectCourseVO> course = staffMapper.selectCourseById(u.getUid());
            StaffSelectUserAndCourseVO result = new StaffSelectUserAndCourseVO();
            BeanUtils.copyProperties(u, result);
            result.setCourses(course);

            userAndCourse.add(result);
        }

        Integer total = staffMapper.getTotal(staffQuery); // 获取总记录数
        StaffUserPageVO pageVO = new StaffUserPageVO();
        pageVO.setUserList(userAndCourse);
        pageVO.setTotal(total);

        log.debug("查询用户信息成功，结果：{}", pageVO);
        return pageVO;
    }

    /**
     * 修改器械状态功能
     * @param update
     * @return boolean
     */
    public boolean updateStatus(StaffUpdateEquipmentParam update){
        log.debug("开始处理修改用户状态请求，参数：{}",update);
        int rows = staffMapper.updateStatus(update);
        log.debug("修改用户状态成功，受影响的行数：{}", rows);
        return rows > 0;
    }

    /**
     * 新增维修记录功能
     * @param add
     * @return boolean
     */
    public boolean addRepairRecord(StaffAddRepairStatus add){
        log.debug("开始处理新增维修记录请求，参数：{}",add);
        int rows = staffMapper.addRepairRecord(add);
        log.debug("新增维修记录成功，受影响的行数：{}", rows);
        return rows > 0;
    }

    /**
     * 查询所有器械功能
     * @param query
     * @return
     */
    @Override
    public StaffEquipmentPageVO getAllEquipmentByPage(StaffEquipmentQuery query) {
        query.setPageNum((query.getPageNum() - 1) * query.getPageSize());
        List<StaffSelectEquipmentVO> result = staffMapper.selectAllEquipmentByPage(query);
        StaffEquipmentPageVO pageVO = new StaffEquipmentPageVO();
        pageVO.setTotal(staffMapper.getTotalEquipment(query));
        pageVO.setEquipmentList(result);
        log.debug("查询所有器械成功，结果：{}", result);
        return pageVO;
    }

    /**
     * 查询所有维修记录功能
     * @param query
     * @return
     */
    @Override
    public StaffRepairPageVO getAllRepairsByPage(StaffRepairQuery query) {
        query.setPageNum((query.getPageNum() - 1) * query.getPageSize());
        List<StaffRepairRecordVO> result = staffMapper.selectAllRepairRecordsByPage(query);

        StaffRepairPageVO pageVO = new StaffRepairPageVO();
        pageVO.setRepairList(result);
        pageVO.setTotal(staffMapper.getTotalRepairRecords(query));

        log.debug("查询所有维修记录成功，结果：{}", result);
        return pageVO;
    }

    /**
     * 修改用户头衔功能
     * @param param
     * @return
     */
    @Override
    public boolean updateUserTitle(StaffUpdateUserTitleParam param) {
        int rows = staffMapper.updateUserTitle(param);
        log.debug("修改用户头衔成功，受影响的行数：{}", rows);
        return rows > 0;
    }

    @Override
    public boolean addUser(StaffAddUserParam dto) {
        int rows = staffMapper.addUser(dto);
        log.debug("新增用户成功，受影响的行数：{}", rows);
        return rows > 0;
    }

}
